home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / infodata / callbook.tar / callbook_1.3 / callbook.c next >
C/C++ Source or Header  |  1991-08-27  |  6KB  |  361 lines

  1. /*
  2.  * Telnet callsign server sources copyright 1989 by Devon Bowen, KA2NRC.
  3.  * You may distribute and modify these files as you please as long as
  4.  * as long as credit to the original creator is given. Please report any
  5.  * bug fixes or modification to bowen@cs.buffalo.edu.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <syslog.h>
  11. #include <netdb.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <sys/time.h>
  15. #include <netinet/in.h>
  16. #include <arpa/telnet.h>
  17.  
  18. #include "callbook.h"
  19.  
  20. int output=0;                /* set output to data mode */
  21. char *param(), tgetchar();
  22.  
  23. extern int max_row;
  24. extern int (*p_routine[])();        /* array of printer routines */
  25.  
  26. main()
  27. {
  28.     char cmd[BUFSIZ];
  29.     int catch_size, quit=0;
  30.     struct sockaddr_in catch_host;
  31.  
  32.     openlog("callsign", LOG_PID, LOG_USER);
  33.  
  34.     catch_size = sizeof(struct sockaddr_in);
  35.  
  36.         if (!getpeername(1, (struct sockaddr_in *) &catch_host, &catch_size))
  37.         syslog(LOG_INFO, "starting from %s",
  38.                     inet_ntoa(catch_host.sin_addr));
  39.  
  40.     tinit();
  41.  
  42.     tputs("Callbook v1.3  Bug reports to bowen@cs.buffalo.edu  Type ", 0);
  43.     tputs("'help' for help\n>> ", 1);
  44.  
  45.     while (!quit) {
  46.         char *cmdstr;
  47.  
  48.         if (tgets(cmd, BUFSIZ-1) == NULL)
  49.             tputs("Command line overflow\n", 0);
  50.  
  51.         else if (paramnum(cmd) == -1)
  52.             tputs("Bad input\n", 0);
  53.  
  54.         else if (!(cmdstr = param(cmd, 0)))
  55.             ;
  56.  
  57.         else if (!strcasecmp(cmdstr, "help"))
  58.             help(cmd);
  59.  
  60.         else if (!strcasecmp(cmdstr, "info"))
  61.             info(cmd);
  62.  
  63.         else if (!strcasecmp(cmdstr, "set"))
  64.             set(cmd);
  65.  
  66.         else if (!strcasecmp(cmdstr, "more"))
  67.             more(cmd);
  68.  
  69.         else if (!strcasecmp(cmdstr, "name"))
  70.             name(cmd);
  71.  
  72.         else if (!strcasecmp(cmdstr, "city"))
  73.             city(cmd);
  74.  
  75.         else if (!strcasecmp(cmdstr, "zip"))
  76.             zip(cmd);
  77.  
  78.         else if (!strcasecmp(cmdstr, "call"))
  79.             call(cmd);
  80.  
  81.         else if (!strcasecmp(cmdstr, "quit"))
  82.             quit = 1;
  83.  
  84.         else
  85.             tputs("Unknown command\n", 0);
  86.  
  87.         if (!quit)
  88.             tputs(">> ", 1);
  89.     }
  90.  
  91.     sleep(1);
  92. }
  93.  
  94.  
  95. help(cmd)
  96. char *cmd;
  97. {
  98.     FILE *fd;
  99.     int para;
  100.     char *filename;
  101.     char buf[BUFSIZ];
  102.  
  103.     if ((para = paramnum(cmd)) == -1) {
  104.         tputs("Bad input\n", 0);
  105.         return;
  106.     }
  107.  
  108.     if (para > 2) {
  109.         tputs("Usage: help [command]\n", 0);
  110.         return;
  111.     }
  112.  
  113.     if (para == 1)
  114.         filename = HELP_FILE;
  115.  
  116.     else {
  117.         strcpy(buf, HELP_PREFIX);
  118.         strcat(buf, param(cmd, 1), 50);
  119.         filename = buf;
  120.     }
  121.  
  122.     if ((fd = fopen(filename, "r")) == NULL) {
  123.         tputs("No help available\n", 0);
  124.         return;
  125.     }
  126.  
  127.     while (fgets(buf, BUFSIZ-1, fd) != NULL)
  128.  
  129.         if (tputs(buf, 0))
  130.             break;
  131.  
  132.     fclose (fd);
  133. }
  134.  
  135.  
  136. info(cmd)
  137. char *cmd;
  138. {
  139.     FILE *fd;
  140.     int para;
  141.     char *buf[BUFSIZ];
  142.  
  143.     if ((para = paramnum(cmd)) == -1) {
  144.         tputs("Bad input\n", 0);
  145.         return;
  146.     }
  147.  
  148.     if (para != 1) {
  149.         tputs("Usage: info\n", 0);
  150.         return;
  151.     }
  152.  
  153.     if ((fd = fopen(INFO_FILE, "r")) == NULL) {
  154.         tputs("Info file unavailable\n", 0);
  155.         return;
  156.     }
  157.  
  158.     while (fgets(buf, BUFSIZ-1, fd) != NULL)
  159.  
  160.         if (tputs(buf, 0))
  161.             break;
  162.  
  163.     fclose (fd);
  164. }
  165.  
  166.  
  167. set(cmd)
  168. char *cmd;
  169. {
  170.     int para;
  171.     char *arg;
  172.  
  173.     if ((para = paramnum(cmd)) == -1) {
  174.         tputs("Usage: set name|data|raw|addr\n", 0);
  175.         return;
  176.     }
  177.  
  178.     if (para != 2) {
  179.         tputs("Usage: set name|data|raw|addr\n", 0);
  180.         return;
  181.     }
  182.  
  183.     arg = param(cmd, 1);
  184.  
  185.     if (!strcasecmp(arg, "data")) {
  186.         output = 0;
  187.         tputs("Output format is data\n", 0);
  188.  
  189.     } else if (!strcasecmp(arg, "name")) {
  190.         output = 1;
  191.         tputs("Output format is name\n", 0);
  192.  
  193.     } else if (!strcasecmp(arg, "raw")) {
  194.         output = 2;
  195.         tputs("Output format is raw\n", 0);
  196.  
  197.     } else if (!strcasecmp(arg, "addr")) {
  198.         output = 3;
  199.         tputs("Output format is addr\n", 0);
  200.  
  201.     } else
  202.         tputs("Output format unknown\n", 0);
  203. }
  204.  
  205.  
  206. more(cmd)
  207. char *cmd;
  208. {
  209.     int para;
  210.     char *arg;
  211.  
  212.     if ((para = paramnum(cmd)) == -1) {
  213.         tputs("Usage: more rows\n", 0);
  214.         return;
  215.     }
  216.  
  217.     if (para != 2) {
  218.         tputs("Usage: more rows\n", 0);
  219.         return;
  220.     }
  221.  
  222.     max_row = atoi(param(cmd, 1));
  223. }
  224.  
  225.  
  226. name(cmd)
  227. char *cmd;
  228. {
  229.     int count, found_data=0, found_arg=0;
  230.     char state[BUFSIZ], *arg;
  231.  
  232.     if (paramnum(cmd) == -1) {
  233.         tputs("Bad input\n", 0);
  234.         return;
  235.     }
  236.  
  237.     if (filtsetup(cmd))
  238.         return;
  239.  
  240.     for (count=1; arg = param(cmd, count); count++)
  241.  
  242.         if (*arg != '-') {
  243.             found_arg = 1;
  244.             found_data += lookup(1, arg, p_routine[output]);
  245.         } else
  246.             count++;
  247.  
  248.     if (!found_arg) {
  249.         tputs("Usage: name [filters] surname ...\n", 0);
  250.         return;
  251.     }
  252.  
  253.     if (!found_data) {
  254.         tputs("No references found\n", 0);
  255.         return;
  256.     }
  257. }
  258.  
  259.  
  260. city(cmd)
  261. char *cmd;
  262. {
  263.     int count, found_data=0, found_arg=0;
  264.     char state[BUFSIZ], *arg;
  265.  
  266.     if (paramnum(cmd) == -1) {
  267.         tputs("Bad input\n", 0);
  268.         return;
  269.     }
  270.  
  271.     if (filtsetup(cmd))
  272.         return;
  273.  
  274.     for (count=1; arg = param(cmd, count); count++)
  275.  
  276.         if (*arg != '-') {
  277.             found_arg = 1;
  278.             found_data += lookup(9, arg, p_routine[output]);
  279.         } else
  280.             count++;
  281.  
  282.     if (!found_arg) {
  283.         tputs("Usage: city [filters] cityname ...\n", 0);
  284.         return;
  285.     }
  286.  
  287.     if (!found_data) {
  288.         tputs("No references found\n", 0);
  289.         return;
  290.     }
  291. }
  292.  
  293.  
  294. zip(cmd)
  295. char *cmd;
  296. {
  297.     int count, found_data=0, found_arg=0;
  298.     char state[BUFSIZ], *arg;
  299.  
  300.     if (paramnum(cmd) == -1) {
  301.         tputs("Bad input\n", 0);
  302.         return;
  303.     }
  304.  
  305.     if (filtsetup(cmd))
  306.         return;
  307.  
  308.     for (count=1; arg = param(cmd, count); count++)
  309.  
  310.         if (*arg != '-') {
  311.             found_arg = 1;
  312.             found_data += lookup(11, arg, p_routine[output]);
  313.         } else
  314.             count++;
  315.  
  316.     if (!found_arg) {
  317.         tputs("Usage: zip [filters] zipcode ...\n", 0);
  318.         return;
  319.     }
  320.  
  321.     if (!found_data) {
  322.         tputs("No references found\n", 0);
  323.         return;
  324.     }
  325. }
  326.  
  327.  
  328. call(cmd)
  329. char *cmd;
  330. {
  331.     int count, found_data=0, found_arg=0;
  332.     char state[BUFSIZ], *arg;
  333.  
  334.     if (paramnum(cmd) == -1) {
  335.         tputs("Bad input\n", 0);
  336.         return;
  337.     }
  338.  
  339.     if (filtsetup(cmd))
  340.         return;
  341.  
  342.     for (count=1; arg = param(cmd, count); count++)
  343.  
  344.         if (*arg != '-') {
  345.             found_arg = 1;
  346.             found_data += lookup(0, arg, p_routine[output]);
  347.             found_data += lookup(16, arg, p_routine[output]);
  348.         } else
  349.             count++;
  350.  
  351.     if (!found_arg) {
  352.         tputs("Usage: call [filters] callsign ...\n", 0);
  353.         return;
  354.     }
  355.  
  356.     if (!found_data) {
  357.         tputs("No references found\n", 0);
  358.         return;
  359.     }
  360. }
  361.